﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Pdf
{
    public class QuestionnaireModule : BaseModule
    {
        public QuestionnaireModule(IMetaProvider overviewProvider, Document pdfDocument, PdfFonts fontsProvider, BookConfiguration cfg)
            : base(overviewProvider, pdfDocument, fontsProvider, cfg)
        {
            HierarchyNode rootNode = metaProvider.GetRootNode();

            // Title
            tables.Add(new PdfBookPTable(TableTitle(rootNode.Name), true));
            TableParagraph();

            foreach (HierarchyNode nodeQuest in rootNode.Nodes)
            {
                tables.Add(new PdfBookPTable(MakeTableVarDetail(nodeQuest, "")));
                TableParagraph();
            }
        }

        private PdfPTable MakeTableVarDetail(HierarchyNode node, string paragraph)
        {
            PdfPTable table = CreatePropertyTable();

            // Question Id
            Phrase ph = new Phrase(paragraph + " Q: " + node.Id, BookFonts.FontProperty);
            Anchor target = new Anchor(ph);
            target.Name = metaProvider.FullIdKey + "Q" + node.Id;
            PdfPCell cell = new PdfPCell(target);
            cell.Border = Rectangle.NO_BORDER;
            cell.Colspan = 2;
            cell.BackgroundColor = new BaseColor(BookFonts.COLOR_TABLE_HEAD);

            // cell.PaddingTop = 8f;
            table.AddCell(cell);

            // Question text (if any)
            string txt = node.Name;
            if (string.IsNullOrEmpty(txt) == false)
                MakeRowVarDetail(table, "Question", txt, false);

            // INFO interview
            txt = node.Descr;
            if (string.IsNullOrEmpty(txt) == false)
                MakeRowVarDetail(table, "Info Interview", txt, true);

            HierarchyNode conceptNode = node.Nodes[0];
            if (string.IsNullOrEmpty(conceptNode.Id) == false)
            {
                txt = conceptNode.Id + " - " + conceptNode.Name;
                string anchorRef = "#" + conceptNode.Descr + conceptNode.Id;
                MakeRowVarDetail(table, "Variable", txt, anchorRef, true);

            }

            // has var a codelist that differs of the one used ?
            HierarchyNode varCodelistNode = node.Nodes[3];
            if (string.IsNullOrEmpty(varCodelistNode.Id) == false)
            {
                string id = varCodelistNode.Id.Split('+')[1];
                string anchorRef = "#" + varCodelistNode.Id;
                MakeRowVarDetail(table, "Variable Codes", id + " - " + varCodelistNode.Name, anchorRef, true);
            }

            // Codelist
            HierarchyNode codelistNode = node.Nodes[1];
            if (string.IsNullOrEmpty(codelistNode.Id) == false)
            {
                string codeId = codelistNode.Id.Split('+')[1];

                MakeRowVarDetail(table, "Codes Question", codeId, "#" + codelistNode.Id, false);

                int indexNo = 1;

                PdfPTable tableCodes = CreateFlatCodelistListTable("Code");
                ProduceHierarchyFlatList(tableCodes, codelistNode, 1, string.Empty, ref indexNo);


                //PdfPTable tableCodes = CreateIndentedListTable2("Code", 50);
                //ProduceIndentedList(table, codelistNode, 1, string.Empty, ref indexNo);

                // Embed table of codes
                PdfPCell cellTable = new PdfPCell(tableCodes);
                cellTable.Border = Rectangle.NO_BORDER;
                cellTable.SetLeading(0.0f, 1.2f);
                cellTable.Colspan = 2;
                cellTable.PaddingLeft = 40f;
                cellTable.PaddingTop = 8;
                table.AddCell(cellTable);
            }

            // Quality
            HierarchyNode qualityNode = node.Nodes[2];
            if (string.IsNullOrEmpty(qualityNode.Id) == false)
            {
                string codeId = qualityNode.Id.Split('+')[1];

                MakeRowVarDetail(table, "Quality codes", codeId, "#" + qualityNode.Id, false);

                PdfPTable tableCodes = CreateFlatCodelistListTable("Quality");
                int indexNo = 1;
                ProduceHierarchyFlatList(tableCodes, qualityNode, 1, string.Empty, ref indexNo);

                PdfPCell cellTable = new PdfPCell(tableCodes);
                cellTable.Border = Rectangle.NO_BORDER;
                cellTable.SetLeading(0.0f, 1.2f);
                cellTable.Colspan = 2;
                cellTable.PaddingLeft = 40f;
                cellTable.PaddingTop = 8;
                table.AddCell(cellTable);
            }



            return table;
        }

    }
}
